<?php
declare (strict_types = 1);

namespace app\api;

use app\admin\model\vod\Main;
use think\App;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
use think\Validate;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];


    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;
        trace("请求参数是：".var_export($this->request->param(),true),'info');
        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {
    }

    /**
     * 验证数据
     * @access protected
     * @param  array        $data     数据
     * @param  string|array $validate 验证器名或者验证规则数组
     * @param  array        $message  提示信息
     * @param  bool         $batch    是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v     = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }
    protected function redirect(...$args){
        throw new HttpResponseException(redirect(...$args));
    }

    public function getAllAdmin($int_role=0,$str_key='id',$str_fields='ra.*'){
        $str_where=" where is_del=1 and is_lock=1 ";
        if($int_role){
            $str_where.=" and rar.role_id={$int_role}";
        }
        $str_sql="select {$str_fields} from rbac_admin as ra left join rbac_admin_role as rar on rar.admin_id=ra.id {$str_where}";
        $arr_data=Db::query($str_sql);
        $arr_data=array_item_key_to_self_key($arr_data,$str_key);
        return $arr_data;
    }
    public function getRoleIds($int_admin_id){
        $str_sql="select * from rbac_admin_role where admin_id={$int_admin_id}";
        $arr_data=Db::query($str_sql);
        $arr_role_ids=[];
        foreach($arr_data as $arr_item){
            $arr_role_ids[]=$arr_item['role_id'];
        }
        return $arr_role_ids;
    }
}
